package com.kujie.access.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kujie.access.domain.query.TenantManageQuery;
import com.kujie.access.domain.vo.TenantManageVO;
import com.kujie.access.service.ITenantManageService;
import com.kujie.common.core.domain.R;
import com.kujie.common.core.utils.StringUtils;
import com.kujie.common.core.validate.AddGroup;
import com.kujie.common.core.validate.EditGroup;
import com.kujie.common.encrypt.annotation.ApiEncrypt;
import com.kujie.common.excel.utils.ExcelUtil;
import com.kujie.common.mybatis.core.page.PageQuery;
import com.kujie.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 租户管理 api
 *
 * @author jyy
 * @date 2025-05-26
 */
@RestController
@RequestMapping("/tenantManage")
@RequiredArgsConstructor
@ApiEncrypt(response = true)
public class TenantManageController extends BaseController {

    private final ITenantManageService service;

    /**
     * 获取租户管理分页
     *
     * @param query     入参
     * @param pageQuery 分页入参
     * @return R<?>
     */
    @GetMapping("/getTenantManagePage")
    public R<?> getTenantManagePage(TenantManageQuery query, PageQuery pageQuery) {
        Page<TenantManageVO> result = service.getTenantManagePage(query, pageQuery);
        return R.ok(result);
    }


    @GetMapping("/detail")
    public R<TenantManageVO> detail(Long id) {
        return R.ok(service.detail(id));
    }

    /**
     * 新增租户管理
     *
     * @param query 入参
     * @return R<?>
     */
    @PostMapping("/addTenantManage")
    public R<?> addTenantManage(@Validated(AddGroup.class)
                                @RequestBody TenantManageQuery query) {
        return toAjax(service.addTenantManage(query, true));
    }

    /**
     * 修改租户管理
     *
     * @param query 入参
     * @return R<?>
     */
    @PutMapping("/updateTenantManage")
    public R<?> updateTenantManage(@Validated(EditGroup.class)
                                   @RequestBody TenantManageQuery query) {
        return toAjax(service.updateTenantManage(query, true));
    }

    /**
     * 删除租户管理
     *
     * @param companyUuids 入参
     * @return R<?>
     */
    @DeleteMapping("/deleteTenantManage/{ids}")
    public R<?> deleteTenantManage(@NotEmpty(message = "主键不能为空")
                                   @PathVariable(name = "ids") List<String> companyUuids) {
        return toAjax(service.deleteTenantManage(companyUuids, true));
    }

    /**
     * 导出租户管理
     *
     * @param query    入参
     * @param response 响应
     */
    @PostMapping("/exportTenantManage")
    public void exportTenantManage(@RequestBody TenantManageQuery query, HttpServletResponse response) {
        List<TenantManageVO> result = service.getTenantManageList(query);
        ExcelUtil.exportExcel(result, "企业信息管理", TenantManageVO.class, response);
    }

    /**
     * 导出租户管理模板
     *
     * @param response 响应
     */
    @PostMapping("/exportTenantManageTemplate")
    public void exportTenantManageTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(CollectionUtil.newArrayList(), "企业信息管理导入模板", TenantManageVO.class, response);
    }

    /**
     * 导入租户管理模板
     *
     * @param file 文件流
     * @return R<?>
     */
    @PostMapping(value = "/importTenantManage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> importTenantManage(@RequestPart("file") MultipartFile file) {
        String result = service.importTenantManage(file);
        return StringUtils.isEmpty(result) ? R.fail() : R.ok(result);
    }

}
